package com.hsomandroidapp.ui;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;

import com.hsomandroidapp.R;
import com.hsomandroidapp.base.BaseActivity;
import com.hsomandroidapp.base.BaseAsyncTask;
import com.hsomandroidapp.base.Constants;
import com.hsomandroidapp.base.ICallBack;
import com.hsomandroidapp.base.Result;
import com.hsomandroidapp.base.Task;
import com.hsomandroidapp.entity.Product;

public class ProductDetailActivity extends BaseActivity implements ICallBack{
	private Product product;
	private TextView productName;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.product_detail);
		super.onCreate(savedInstanceState);
		product = (Product)getIntent().getSerializableExtra("product");
		initBaseHeader(product.getContent(), Constants.HEAD_MODE_OTHER);
		findViewById(R.id.btn_buy).setOnClickListener(buyOnClickListener);
		findViewById(R.id.btn_addcart).setOnClickListener(addCartClickListener);
		productName = (TextView)findViewById(R.id.tv_product_name);
		productName.setText(product.getContent());
		( (TextView)findViewById(R.id.tv_price)).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG); 
		
		( (TextView)findViewById(R.id.tv_price)).setText(getString(R.string.str_balance_tag)+product.getPrice());
		( (TextView)findViewById(R.id.tv_real_price)).setText(getString(R.string.str_balance_tag)+product.getRealPrice());
		( (ImageView)findViewById(R.id.iv_head_image)).setBackgroundResource(product.getImageId());

	}
	private OnClickListener buyOnClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			
			HashMap<String, String> param = new HashMap<String, String>();
			param.put("goodsId", product.getProductId());
			param.put("nums", "1");
			Task task = new Task(Task.TASK_ADD_CART2, param);
			new BaseAsyncTask(ProductDetailActivity.this).execute(task);
			
			
			
		}
	};
	
	private OnClickListener addCartClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {
			HashMap<String, String> param = new HashMap<String, String>();
			param.put("goodsId", product.getProductId());
			param.put("nums", "1");
			Task task = new Task(Task.TASK_ADD_CART, param);
			new BaseAsyncTask(ProductDetailActivity.this).execute(task);
			
		}
	};
	
	@Override
	public void call(Result result) {
		 if(Task.TASK_ADD_CART == result.getTaskid()){
			isSuccess(result.getObject(), getString(R.string.str_add_cart_success));
		}else if(Task.TASK_ADD_CART2 == result.getTaskid()){
			Intent intent = new Intent();
			ArrayList<Product> sendData = new ArrayList<Product>();
			sendData.add(product);
			intent.putExtra("products", sendData);
			intent.setClass(ProductDetailActivity.this, ProductInfoActivity.class);
			ProductDetailActivity.this.startActivity(intent);
		}
	};
}
